package com.helloworld.bulletin.controller;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cfw.util.ControllerUtil;
import com.helloworld.bulletin.service.BulletinService;

@Controller
@RequestMapping("/bulletin/*")
public class BulletinController {
	private static final Logger logger = LoggerFactory.getLogger(BulletinController.class); 

	@Autowired 
	private BulletinService bulletinService; 
	
	@RequestMapping(value = "/list.json", method = {RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Map<String, Object> list(@RequestParam final Map<String, Object> param) {
		logger.debug("*** param:" + param);
		return bulletinService.list(param);
	}
	
	@RequestMapping(value = "/read.json", method = {RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Map<String, Object> read(@RequestBody final Map<String, Object> param) {
		if (param != null) {
			logger.debug(param.toString());
		} else {
			logger.debug("param is null");
		}
		return bulletinService.select(param);
	}

	@RequestMapping(value = "/save.json", method = {RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Map<String, Object> save(@RequestBody final Map<String, Object> param) {
		param.put("userId", "ADMIN");
		bulletinService.save(param);
		
		return ControllerUtil.getSuccessMap();
	}

	@RequestMapping(value = "/delete.json", method = {RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Map<String, Object> delete(@RequestBody final Map<String, Object> param) {
		bulletinService.delete(param);
		
		return ControllerUtil.getSuccessMap();
	}
	
}
